@extends('admin.layout')

@section('title', '修改密码')

@section('body')
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">修改密码</div>
                    <div class="layui-card-body layui-row">
                        <form class="layui-form layui-col-lg9">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">当前密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="password_old" required lay-verify="required" placeholder="请输入当前密码"
                                               autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">新密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="password_new" required lay-verify="required" placeholder="请输入新密码"
                                               autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">确认新密码</label>
                                    <div class="layui-input-inline">
                                        <input type="password" name="password_confirm" required lay-verify="required" placeholder="请再次输入新密码"
                                               autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit="">确认修改</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['layer', 'form'], function () {
            var $ = layui.$;
            var layer = layui.layer;
            var form = layui.form;

            form.on('submit', function (obj) {
                if (obj.field.password_new != obj.field.password_confirm) {
                    layer.msg('新密码二次输入不一致', {icon: 5});
                    return false;
                }

                $.post('/admin/password', obj.field, function (res) {
                    if (res.code === 0) {
                        layer.msg("修改成功，请用新密码重新登录。", {
                            icon: 1,
                            time: 3000
                        }, function () {
                            parent.location.href = "/admin/logout";
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }, 'json');

                return false;
            });
        });
    </script>
@endsection
